//
//  FootModel.m
//  project
//
//  Created by 黄建华 on 2018/11/8.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "FoodModel.h"

@implementation FoodEatModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _serial      = 0;
        _caloryTotal = 0.0;
        _date        = [[NSDate date] dateToDayString];
    }
    return self;
}

@end

@implementation FoodModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _user_id       = SN_USER.user_id;
        _stepTarget    = SN_USER.target_step;
        _weight        = SN_USER.weight;
        _goal_weight   = SN_USER.goal_weight;
        _finish        = 1;
        _finishQuality = NO;
        _goal_calory   = SN_USER.goal_calory;
        _baseCalor     = _weight *26;
    }
    return self;
}

+ (FoodModel *)initWithDate:(NSDate *)date {
    FoodModel *model = [[FoodModel alloc] init];
    
    model.user_id       = SN_USER.user_id;
    model.dateString    = [date dateToDayString];
    model.stepTarget    = SN_USER.target_step;
    model.weight        = SN_USER.weight;
    model.goal_weight   = SN_USER.goal_weight;
    model.finish        = 1;
    model.finishQuality = NO;
    model.goal_calory   = SN_USER.goal_calory;
    model.baseCalor     = model.weight *26;
    
    NSDate *ddd = date;
    model.weekCount  = ddd.weekOfYear;
    model.monthCount = ddd.month;
    model.yearCount  = ddd.year;
    return model;
}

- (void)setWeight:(NSInteger)weight {
    _weight        = weight;
    self.baseCalor = _weight *26;
}

+ (FoodModel *)getFoodModelFromDBWithDayString:(NSString *)string {
    NSString *where = [NSString stringWithFormat:@"user_id = '%@' and dateString = '%@'",
                       SN_USER.user_id, string];
    FoodModel *model = [FoodModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model               = [[FoodModel alloc] init];
        model.dateString    = string;
        model.user_id       = SN_USER.user_id;
        model.weight        = SN_USER.weight;
        model.goal_weight   = SN_USER.goal_weight;
        model.finish        = 1;
        model.finishQuality = NO;
        model.goal_calory   = SN_USER.goal_calory;
        model.baseCalor     = model.weight *26;
    }
    if (model.baseCalor == 0) {
        model.baseCalor = SN_USER.weight *26;
    }
    NSDate *ddd = [NSDate dateByString:string];
    model.weekCount  = ddd.weekOfYear;
    model.monthCount = ddd.month;
    model.yearCount  = ddd.year;
    return model;
}

+ (void)updateModelWithDic:(NSDictionary *)dic withDateString:(NSString *)date {
    NSDictionary *dicData = dic;
    NSArray      *foodArray;
    NSString     *dateString;
    FoodModel *model = [FoodModel getFoodModelFromDBWithDayString:date];
    model.lastTime = @"";
    if ([dic isKindOfClass:[NSArray class]]) {
        
    }
    else if([dic isKindOfClass:[NSDictionary class]])
    {
        if([[dicData allKeys] containsObject:@"meals"])
        {
            NSLog(@"存在食谱");
            model.weight      = [dicData[@"weight"]integerValue];
            model.goal_weight = [dicData[@"goal_weight"]integerValue];
            model.goal_calory = [dicData[@"goal_calory"]integerValue];
            foodArray         = dicData[@"meals"];
            dateString        = dicData[@"date"];
        }else{
            NSLog(@"不存在食谱");
        }
    }

    float          calorTotal = 0.0;
    NSMutableArray *arr       = [NSMutableArray array];
    
    for (int i = 0; i < foodArray.count; i++) {
        NSDictionary *dic      = [foodArray objectAtIndex:i];
        FoodEatModel *eatModel = [[FoodEatModel alloc] init];
        eatModel.date          = dic[@"date"];
        eatModel.userId        = [dic[@"id"]integerValue];
        eatModel.serial        = i;
        eatModel.caloryTotal   = 1.0 *[dic[@"calory"]integerValue];
        eatModel.foodListArray = dic[@"foods"];
        calorTotal            += 1.0*eatModel.caloryTotal;
        model.lastTime = dic[@"create_time"];
        [arr addObject:eatModel];
    }
    
    if (model.weight == 0) {
        model.weight = SN_USER.weight;
    }
    
    if (model.goal_weight == 0) {
        model.goal_weight = SN_USER.goal_weight;
    }
    
    if (model.goal_calory == 0) {
        model.goal_calory = SN_USER.goal_calory;
    }
    
    model.baseCalor = model.weight *26;
    model.currentCalor = (NSInteger)calorTotal;
    model.array        = [NSArray arrayWithArray:arr];
    if (calorTotal < model.goal_calory) {
        model.finishQuality = YES;
    } else {
        model.finishQuality = NO;
    }
    [model updateToDB];
    
    SportModel *modelSport = [SportModel getSportModelFromDBWithDayString:model.dateString];
    if (model.currentCalor > 0) {
        modelSport.isFood = YES;
    } else {
        modelSport.isFood = NO;
    }
    
    if ([model.dateString isEqualToString:SN_PublicClass.todayFood.dateString]) {
        SN_PublicClass.todayFood = model;
    }
    
    NSString *createTime = model.lastTime;
    if (createTime.length > 0) {
        NSDate *startTime = [NSDate dateByString:createTime];
        NSDate *date = startTime; // 获得时间对象
        NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
        NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
        NSDate *dateNow = [date dateByAddingTimeInterval:+time];// 然后把差的时间加
//        NSString *s1 = [dateNow dateToDayString];
        NSString *s1 = [dateNow dateToStringHourMin];
        NSString *s2 = [s1 componentsSeparatedByString:@" "][1];
        model.lastTime = s2;
    }

    [modelSport updateToDB];
    [model updateToDB];
}

- (NSInteger)count {
    return _array.count;
}

// 计算目标卡路里
- (void)calorTargetGet {
}

// 表名
+ (NSString *)getTableName {
    return @"FootModel";
}

// 复合主键
+ (NSArray *)getPrimaryKeyUnionArray {
    return @[@"user_id", @"dateString"];
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

+ (void)initialize {
    [super initialize];
}

@end
